﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
  public  class HoaDonThanhToanDAO
    {
      public List<HoaDonThanhToanDTO> DanhSachHoaDonThanhToan()
      {
          List<HoaDonThanhToanDTO> kq = new List<HoaDonThanhToanDTO>();
          DataProvider dp = new DataProvider();
          string strsql = "select*from HoaDonThanhToan";
          dp.Connect();
          SqlDataReader dr = dp.ExecuteQuery(strsql);
          if (dr.HasRows)
          {
              while (dr.Read())
              {
                  HoaDonThanhToanDTO hd = new HoaDonThanhToanDTO();
                  hd.HDTT_ID = int.Parse(dr["HDTT_ID"].ToString());
                  hd.NgayLap=DateTime.Parse(dr["NgayLap"].ToString());
                  hd.PDP_ID = int.Parse(dr["PDP_ID"].ToString());
                  hd.SoHoaDon = dr["SoHoaDon"].ToString();
                  hd.TenDangNhap = dr["TenDangNhap"].ToString();
                  hd.TongTien = float.Parse(dr["TongTien"].ToString());
                  kq.Add(hd);
              }
          }
          dp.Disconnect();

          return kq;
      }
      public HoaDonThanhToanDTO LayHoaDonByID(int hDTT_ID)
      {
          HoaDonThanhToanDTO hd = new HoaDonThanhToanDTO();
          DataProvider dp = new DataProvider();
          string strsql = "Select*from HoaDonThanhToan where HDTT_ID="+hDTT_ID;
          dp.Connect();

          SqlDataReader dr = dp.ExecuteQuery(strsql);
          dr.Read();
          hd.HDTT_ID = int.Parse(dr["HDTT_ID"].ToString());
          hd.NgayLap = DateTime.Parse(dr["NgayLap"].ToString());
          hd.PDP_ID = int.Parse(dr["PDP_ID"].ToString());
          hd.SoHoaDon = dr["SoHoaDon"].ToString();
          hd.TenDangNhap = dr["TenDangNhap"].ToString();
          hd.TongTien = float.Parse(dr["TongTien"].ToString());

          return hd;
      }
      public HoaDonThanhToanDTO LayHoaDonBySoHoaDon(string soHoaDon)
      {
          HoaDonThanhToanDTO hd = new HoaDonThanhToanDTO();
          DataProvider dp = new DataProvider();
          string strsql = "Select*from HoaDonThanhToan where SoHoaDon='" +soHoaDon+"'";
          dp.Connect();

          SqlDataReader dr = dp.ExecuteQuery(strsql);
          dr.Read();
          hd.HDTT_ID = int.Parse(dr["HDTT_ID"].ToString());
          hd.NgayLap = DateTime.Parse(dr["NgayLap"].ToString());
          hd.PDP_ID = int.Parse(dr["PDP_ID"].ToString());
          hd.SoHoaDon = dr["SoHoaDon"].ToString();
          hd.TenDangNhap = dr["TenDangNhap"].ToString();
          hd.TongTien = float.Parse(dr["TongTien"].ToString());

          return hd;
      }
      public Boolean ThemHoaDonThanhToan(HoaDonThanhToanDTO hd)
      {
          Boolean kq = false;
          DataProvider dp = new DataProvider();
          string strsql = "insert into HoaDonThanhToan(SoHoaDon,NgayLap,PDP_ID,TenDangNhap,TongTien)";
          strsql += " values('" + hd.SoHoaDon + "','" + hd.NgayLap + "'," + hd.PDP_ID + ",'" + hd.TenDangNhap + "'," + hd.TongTien + ")";
          dp.Connect();
          int i = dp.ExecuteNonQuery(strsql);
          if (i != 0)
              kq = true;
          return kq;
      }
      public Boolean XoaHoaDonThanhToanByID(int hDTT_ID)
      {
          Boolean kq = false;
          DataProvider dp = new DataProvider();
          string strsql = "delete from HoaDonThanhToan where HDTT_ID="+hDTT_ID;
          dp.Connect();
          int i = dp.ExecuteNonQuery(strsql);
          if (i != 0)
              kq = true;
          return kq;
      }
      public Boolean XoaHoaDonThanhToanBySoHoaDon(string soHoaDon)
      {
          Boolean kq = false;
          DataProvider dp = new DataProvider();
          string strsql = "delete from HoaDonThanhToan where SoHoaDon='" +soHoaDon+ "'";
          dp.Connect();
          int i = dp.ExecuteNonQuery(strsql);
          if (i != 0)
              kq = true;
          return kq;
      }
      public Boolean CapNhatHoaDonThanhToan(HoaDonThanhToanDTO hd)
      {
          Boolean kq = false;
          DataProvider dp = new DataProvider();
          string strsql = "update HoaDonThanhToan set SoHoaDon='"+hd.SoHoaDon+"' and NgayLap='"+hd.NgayLap+"' and PDP_ID="+hd.PDP_ID+" and TenDangNhap='"+hd.TenDangNhap+"' and TongTien="+hd.TongTien+;
          strsql=" where HDTT_ID="+hd.HDTT_ID;
          dp.Connect();
          int i = dp.ExecuteNonQuery(strsql);
          if (i != 0)
              kq = true;
          return kq;
      }

    }
}
